var loanId = $("#loan").data("id"),
    loanStatus = $("#loan").data("status"),sysSalesman ={};

$(function () {
    var showContract = ['LOANED', 'CLEARED'];
    /**
     * 加载标的投标记录
     */
    var loadInvests = function () {
        var lang = chinese;
        chinese.sInfo = "共 _TOTAL_ 人投标";
        chinese.sInfoEmpty = "";
        // 初始化列表
        var table = $('#investList').DataTable({
            //"processing": true,
            //"serverSide": true,
            "ajax": "bill/getInvestData/" + loanId,
            "columns": [
                {"data": "id"},
                {"data": "userLoginName"},
                {"data": "salesmanId"},
                {"data": "recommendUser"},
                {"data": "userName"},
                {"data": "amount"},
                {"data": "investTime"},
                {"data": "status"},
                {"data": "id"}
            ],
            "aoColumnDefs": [{
                    "targets": 1, //投资人
                    "mRender": function (data, type, row) {
                        return data === ''
                                ? '未知'
                                : '<a href="user/index/' + row.userId + '" target="_blank">' + data + '</a>';
                    }
                },{
                    "targets": 2, //理财顾问
                    "mRender": function (data, type, row) {
                        var salesman = sysSalesman[data];
                        return (salesman === undefined)
                            ? data
                            : salesman;
                    }
                },{
                    "targets": 3, //推荐人
                    "mRender": function (data, type, row) {
                        if (row.recommendUserId)
                        {
                            return '<a href="user/index/' + row.recommendUserId + '" target="_blank">'+ (row.userId == row.recommendUserId ? "(无效)" : "") + row.recommendUser + '</a>';
                        }
                        return "";
                    }
                }, {
                    "targets": 4, //姓名
                    "mRender": function (data, type, row) {
                        return data ? data : '-';
                    }
                }, {
                    "targets": 5, //金额
                    "mRender": function (data, type, row) {
                        return $.number(data);
                    }
                }, {
                    "targets": 6, //投标时间
                    "searchable": false,
                    "mRender": function (data, type, row) {
                        return $.format.date(new Date(data), "yyyy/MM/dd HH:mm");
                    }
                }, {
                    "targets": 7, //状态
                    "searchable": false,
                    "mRender": function (data, type, row) {
                        return sysInvestStatus[data];
                    }
                }, {
                    "targets": 8, //合同
                    "searchable": false,
                    "sortable": false,
                    "visible": loanStatus === 'LOANED' || loanStatus === 'CLEARED',
                    "mRender": function (data, type, row) {
                        if (loanStatus === 'LOANED' || loanStatus === 'CLEARED') {
                            return '<a href="contract/invesorContractByJson/' + loanId + '/' + data + '" target="_blank">查看合同</a>';
                        } else {
                            return '';
                        }
                    }
                }],
            "order": [5, 'desc'],
            "oLanguage": chinese,
            "language": {
                "decimal": ",",
                "thousands": ",",
            },
            "iDisplayLength": 25,
            "aLengthMenu": [
                [5, 10, 25, 50, 100, -1],
                [5, 10, 25, 50, 100, "全部"]
            ],
            "sDom": '<"dt-panelmenu clearfix"<"invest-table-title pull-left mt5 fw600"><"pull-right"T>r>t<"dt-panelfooter clearfix"lp>',
            "oTableTools": {
                "aButtons": [{
                        "sExtends": "xls",
                        "sButtonText": "导出为Excel"
                    }
                ],
                "sSwfPath": root_path + "/js/plugins/datatables/extensions/TableTools/swf/copy_csv_xls_pdf.swf"
            },
            "initComplete": function (settings, json) {
                $(".invest-table-title").html('<i class="fa fa-rocket mr5"></i><span class="fs14">标的投标记录</span><code class="investCount">共' + json.recordsTotal + '人</code>');
            }
        });

        table.on('order.dt search.dt', function () {
            table.column(0, {search: 'applied', order: 'applied'}).nodes().each(function (cell, i) {
                cell.innerHTML = i + 1;
            });
        }).draw();
    };

    var initPage = function () {
        // 更新tab状态
        $("#tab-invests").removeClass("btn-default").addClass("btn-primary");
        // 更新标的列表链接
        $(".returnLoanListLink").attr("href", "bill/list/" + loanStatus.toLowerCase());
        initSalemans();
        // 加载投标记录
        loadInvests();
    };

    // 初始化页面
    initPage();
});

var initSalemans = function () {
    $.get("salesman/getSalesmanData", function (res) {
        var data = res.data;
        if (null !== data) {
            for (var i = 0; i < data.length; i++) {
                sysSalesman[data[i].id] = data[i].name;
            }
        }
    });
}
